//
//  TBButton.swift
//  cxd4iphone
//
//  Created by hexy on 12/25/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

class TBButton: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        imageView?.contentMode = .Center
        titleLabel?.textAlignment = .Center
        //        titleLabel?.font = xx_fontOfSize(size: xx_folat(12))
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
        
        let titleX: CGFloat = 0.0
        let titleY: CGFloat = contentRect.size.height * 0.5
        let titleWidth: CGFloat = contentRect.size.width
        let titleHeight: CGFloat = contentRect.size.height * 0.6
        
        return CGRectMake(titleX, titleY, titleWidth, titleHeight)
    }
    
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
        
        let imageX: CGFloat = 0.0
        let imageY: CGFloat = 0.0
        let imageWidth: CGFloat = contentRect.size.width
        let iamgeHeight: CGFloat = contentRect.size.height * 0.7
        return CGRectMake(imageX, imageY, imageWidth, iamgeHeight)
    }

    
}


extension TBButton {
    
    convenience init(title: String, image: UIImage) {
        self.init()
        
        titleLabel?.textAlignment = .Center
        titleLabel?.font = xx_fontOfSize(size: 12)
        titleLabel?.sizeToFit()
        backgroundColor = UIColor.whiteColor()
        
        setTitle(title, forState: .Normal)
        setTitleColor(xx_colorWithHex(hexValue: 0x333333), forState: .Normal)
        setImage(image, forState: .Normal)
    }
}

